package com.boe.hbd.client.activity;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.boe.hbd.client.Constants;
import com.boe.hbd.client.R;
import com.boe.hbd.client.contextmenu.IconContextMenu.IconContextItemSelectedListener;
import com.boe.hbd.client.domain.MediaEntity;
import com.boe.hbd.client.service.LandscapeService;
import com.boe.hbd.client.service.ServiceManager;

public class LandscapeVoicesActivity extends ListActivity implements OnClickListener,SimpleAdapter.ViewBinder,TextWatcher, IconContextItemSelectedListener { 
    private LandscapeService landscapeService= null ; 
    private SimpleAdapter mAdapter           = null ;
    private SimpleDateFormat dateFormat      = null ;
    private TextView mFilter                 = null ;    
    private Long landscapeId                 = null ;
    private List<MediaEntity> mediaEntities  = null ;
    
	public void onCreate(Bundle theInstance) {
		// STEP 1: initialize the UI
		super.onCreate(theInstance);
		landscapeId = getIntent().getExtras().getLong(Constants.KEY_LANDSCAPE_ID); 
		setContentView(R.layout.landscape_voices_activity);   
		findViewById(R.id.recordvoice).setOnClickListener(this); 
		// STEP 2: initialize the service instances 
		landscapeService = ServiceManager.getInstance(null).getService(LandscapeService.class); 
		dateFormat       = new SimpleDateFormat(getResources().getString(R.string.date_format));
		// STEP 3: build the list adapter
		getListView().setItemsCanFocus(true);
		registerForContextMenu(getListView());
		// STEP 4:  initialize the filter search box
        mFilter = (TextView) findViewById(R.id.searchFilter);
        if (mFilter != null) {
            mFilter.addTextChangedListener(this);
        }    	 
        // STEP 6: retrieve data from back end
        refreshListView();
	}

	public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    }

	public void onTextChanged(CharSequence s, int start, int before, int count) { 
    }

	public void afterTextChanged(Editable s) { 
		refreshListView();
    }

	public void onClick(View v) {
		LandscapeHelper.processRequestForLandscape(this, landscapeId, v.getId());
    }

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if ( LandscapeHelper.processResultForLandscape(this, requestCode, data, landscapeId)){
				refreshListView();
			}
		}
	}
	public boolean setViewValue(View view, final Object data, String text) {
		if (view.getId() == R.id.row_gmt_created) {
			String gmtCreated = dateFormat.format(new Date((Long) data));
			((TextView) view).setText(gmtCreated);
			return true;
		} else if (view.getId() == R.id.row_created_by) {
			String createdBy = getResources().getString(R.string.created_by_unknown);
			if (data != null && !data.equals("")) {
				createdBy = data.toString();
			}
			((TextView) view).setText(createdBy);
			return true;
		} else if (view.getId() == R.id.row_options_button) {
			view.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					LandscapeHelper.buildContextMenu(LandscapeVoicesActivity.this, LandscapeVoicesActivity.this, data, R.menu.voices_of_landscape);
				}
			});
			return true;
		} else if (view.getId() == R.id.row_icon ) {
			view.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Uri uri = Uri.parse(data.toString());
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent);
				}
			});
			return true;
		}
		return false;
	}

	public void onIconContextItemSelected(MenuItem item, Object data) {
		if (data != null) {
			Long mediaId = (Long)data;
			if (item.getItemId() == R.id.deleteVoice ) {
				if (LandscapeHelper.removeEntity(this,mediaId)){
					refreshListView();
				}
			}
		}
	}
	
	protected void refreshListView() { 
		mediaEntities = landscapeService.findVoicesByFilterOfLandscape(landscapeId,mFilter.getText().toString());
		mAdapter = new SimpleAdapter(
			  this
		    , mediaEntities
		    , R.layout.landscape_voices_list_item
		    , new String[]{ "uri","gmtCreated" , "createdBy" , "name" , "id" }
			, new int[]{R.id.row_icon,R.id.row_gmt_created,R.id.row_created_by,R.id.row_title,R.id.row_options_button});
		mAdapter.setViewBinder(this);
		setListAdapter(mAdapter); 
	}
}
